package com.dfbz.dao;

import com.dfbz.dao.basedao.BaseDao;
import com.dfbz.model.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class UserDao extends BaseDao<User> {
    private JdbcTemplate jdbcTemplate = getJdbcTemplate();
    public UserDao(Class<User> userClass) {
        super(userClass);
    }

    public List<User> findPage(String realName, Integer startIndex, Integer pageSize) {
        List<User> userList = jdbcTemplate.query("select * from user where real_name like ? order by login_time desc limit ?,? ",
                          new BeanPropertyRowMapper<>(User.class), "%" + realName + "%", startIndex, pageSize);
        return userList;
    }
    public Integer countByRealName(String realName){
        Integer integer = jdbcTemplate.queryForObject("select count(1) from user where real_name like ?", Integer.class, "%" + realName + "%");
        return integer;
    }

    public List<User> toLogin(String username,String password){

        List<User> list = jdbcTemplate.query("select *from user where username=? and password=? ", new BeanPropertyRowMapper<>(User.class), username, password);
        if (list.size()>0){
            return list;
        }else {
            return null;
        }

    }

    public boolean toRegister(User user) {
        int row = jdbcTemplate.update("insert into user (username,password) values (?,?);", user.getUsername(), user.getPassword());
        return row>0;
    }

    public List<User> findUser(Integer startIndex, Integer pageSize, String username) {
        List<User> list = null;
        if (username!=null&username!=""){
            String rname="%" + username + "%";
            list=jdbcTemplate.query( "select * from user where real_name like ? limit ?,?", new BeanPropertyRowMapper<>(User.class),rname,startIndex,pageSize);
        }else {
            list = jdbcTemplate.query( "select * from user limit ?,?", new BeanPropertyRowMapper<>(User.class),startIndex,pageSize);
        }
        return list;
    }
    public Integer findAll(){
        Integer count = jdbcTemplate.queryForObject("select count(*) from user ", Integer.class);
        return count;
    }

    public Integer findUserMessage(Long id) {
        String sql ="SELECT COUNT(1) FROM favorite WHERE u_id="+id;
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        return integer;
    }

    public List<Map<String, Object>> getMyFocus(Integer id,Integer starPage,Integer pageNum) {
        String sql="SELECT u.real_name,u.id \n" +
                          "FROM user u \n" +
                          "LEFT JOIN userfocus us on u.id=us.user_focus_id\n" +
                          "WHERE us.user_id=? limit ?,?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,id,starPage,pageNum);
        return maps;
    }

    public List<Map<String, Object>> getAllMyFocus(Long id) {
        String sql="SELECT u.real_name,u.id \n" +
                          "FROM user u \n" +
                          "LEFT JOIN userfocus us on u.id=us.user_focus_id\n" +
                          "WHERE us.user_id=?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,id);
        return maps;
    }



    public Integer findAllMyFocus(Integer id) {
        String sql="SELECT u.real_name,u.id \n" +
                          "FROM user u \n" +
                          "LEFT JOIN userfocus us on u.id=us.user_focus_id\n" +
                          "WHERE us.user_id=?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, id);
        return maps.size();
    }

    public void cancelFocus(int userId, int foucsId) {
        String sql="DELETE FROM userfocus WHERE user_id=? AND user_focus_id=?";
        int update = jdbcTemplate.update(sql, userId, foucsId);
    }

    public Integer befocus(long messageId) {
        String sql="SELECT COUNT(1) FROM userfocus WHERE user_focus_id="+messageId;

        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);

        return integer;
    }

    public void addLook(long messageId) {
        String sql="UPDATE user set look=look+1 WHERE id=?";
        jdbcTemplate.update(sql,messageId);
    }
}
